﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Salon
{
    public partial class DictBrand : Form
    {
        public DictBrand()
        {
            InitializeComponent();
            dict = ServiceFactory.getDictService();
        }

        private IList<DictionaryItem> list { get; set; }
        private DictService dict { get; set; }

        // metoda podpięta pod event TextChange elementu txtElem - 
        //      odblokowuje przycisk "Dodaj"
        //
        // cel: uniemozliwienie dodawania pustego elementu do listy
        //
        private void txtElem_TextChanged(object sender, EventArgs e)
        {
            Refresh_Buttons();
        }

      
        private void lstView_ItemSelected(object sender, EventArgs e) 
        {
            if (this.lstView.SelectedItem != null)
            {
                this.btnDel.Enabled = true;
                this.txtElem.Text = dict.getMarkById((int)lstView.SelectedValue).DictValue;
                txtId.Text = lstView.SelectedValue.ToString();
            }

            Refresh_Buttons();
        }


        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadMarks();
        }

        private void LoadMarks()
        {
            IList<DictionaryItem> list = ServiceFactory.getDictService().getMarks(new Filter("active", 1).asList());

            lstView.SelectedItem = null;
            lstView.DataSource = list;
            lstView.DisplayMember = "DictValue";
            lstView.ValueMember = "Id";

            lstView.SelectedItem = null;
            txtId.Text = "";
            txtElem.Text = "";
            Refresh_Buttons();
        }

        private void Refresh_Buttons()
        {
            if (txtId.Text != "")
            {
                btnAdd.Visible = btnAdd.Enabled = false;
                btnEdit.Visible = btnEdit.Enabled = true;
                btnDel.Enabled = true;

                labelId.ForeColor = txtId.ForeColor = Color.Black;
            }
            else
            {
                btnAdd.Visible = true;
                btnAdd.Enabled = true;//(txtElem.Text != "");
                btnEdit.Visible = btnEdit.Enabled = false;
                btnDel.Enabled = false;

                labelId.ForeColor = txtId.ForeColor = Color.Gray;
            }
        }

        private void btnDel_Click_1(object sender, EventArgs e)
        {
            IList<DictionaryItem> list = this.lstView.DataSource as List<DictionaryItem>;
            if (list != null)
            {

                DictionaryItem item = new DictionaryItem();
                item.Id = (int)lstView.SelectedValue;
                dict.deleteMark(item);
                LoadMarks();
            }


            Refresh_Buttons();
        }

        private void btnAdd_Click_1(object sender, EventArgs e)
        {
            if (txtElem.Text.Length > 0)
            {

                DictionaryItem item = new DictionaryItem();
                item.DictValue = txtElem.Text;

                int state = dict.addMark(item);

                if (state == DictService.ACTIVATED)
                    MessageBox.Show("Ta marka była już w bazie danych i została ponownie aktywowana!");

                if (state == DictService.ERROR)
                    MessageBox.Show("Ta marka jest już w bazie danych");


                LoadMarks();
            }
        }

        private void DictBrand_Load(object sender, EventArgs e)
        {
            btnEdit.Left = btnAdd.Left;
            btnEdit.Top = btnAdd.Top;

            LoadMarks();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            lstView.SelectedItem = null;
            txtId.Text = "";
            txtElem.Text = "";
            Refresh_Buttons();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtElem.Text.Length > 0)
            {
                //DictService ds = ServiceFactory.getDictService();
                DictionaryItem item = new DictionaryItem();
                item.DictValue = txtElem.Text;
                item.Id = Convert.ToInt32(txtId.Text);
                int state = dict.updateMark(item);

                if (state == DictService.ERROR)
                    MessageBox.Show("Błąd aktualizacji marki");

                LoadMarks();
            }
        }

        
    }
}
